<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\user\Ranking;

class Rank extends Api
{
    protected $noNeedLogin = "*";
    protected $noNeedRight = "*";

    public function index()
    {
        $inviter_num_list = Ranking::alias("r")->join('user u', 'r.user_id = u.id')
            ->where('r.type', "inviter_num")->field("u.nickname,u.avatar,r.num,r.type")
            ->order('r.num DESC')
            ->limit(3)->select();
        $rank_num_list = Ranking::alias("r")->join('user u', 'r.user_id = u.id')
            ->where('r.type', "rank_num")->field("u.nickname,u.avatar,r.num,r.type")
            ->order('r.num DESC')
            ->limit(3)->select();
        $win_rate_list = Ranking::alias("r")->join('user u', 'r.user_id = u.id')
            ->where('r.type', "win_rate")->field("u.nickname,u.avatar,r.num,r.type")
            ->order('r.num DESC')
            ->limit(3)->select();
        $inviter_num = Ranking::alias("r")->join('user u', 'r.user_id = u.id')
            ->where('r.type', "inviter_num")->where('u.status', "normal")->sum("r.num");
        $rank_num = Ranking::alias("r")->join('user u', 'r.user_id = u.id')
            ->where('r.type', "rank_num")->where('u.status', "normal")->sum("r.num");
        $win_rate = Ranking::alias("r")->join('user u', 'r.user_id = u.id')
            ->where('r.type', "win_rate")->where('u.status', "normal")->avg("r.num");
        $this->success("", [
            'inviter_num' => ['list' => $inviter_num_list, 'num' => $inviter_num],
            'rank_num' => ['list' => $rank_num_list, 'num' => $rank_num],
            'win_rate' => ['list' => $win_rate_list, 'num' => $win_rate]
        ]);
    }
}